import 'package:flutter/material.dart';
import '../static_var.dart';
import '../utils.dart';
import '../widget/loading_gradient_btn.dart';
import '../model/ex_stream.dart';
import '../model/cus_dialog.dart';
import '../user_proof.dart';
import '../widget/radio_group.dart';

class PageRegist extends StatefulWidget {

  // TextEditingController _unameController= TextEditingController();
  // TextEditingController _pwdController= TextEditingController();
  
  @override
  _StatePageRegist createState() {
    return _StatePageRegist();
  }
}
class _StatePageRegist extends State<PageRegist> {
  String sexValue = '1';
  ExStream btnLoadingStream = ExStream<bool>(false);

  String userName = '';
  String pwd='';

  void regist(BuildContext context) async {
    bool btnStreamData = this.btnLoadingStream.data;
    if(null == btnStreamData || !btnStreamData) {
      if('' != userName && '' != pwd) {
        btnLoadingStream.setData(true);
        Utils.post(context: context, url: StaticVar.remoteUrl + '/regist', data: {'uname': userName, 'sex':this.sexValue, 'pwd': pwd},
          success: (res){
            UserProof.setUserId(res['data']['id']);
            Navigator.pushReplacementNamed(context, '/lobby');
          }, finnaly: (){
            btnLoadingStream.setData(false);
          }
        );
      }else {
        CusDialog.showError(context, '昵称和密码不能为空');
      }
    }
  }
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.center,
        width: 300,
        child: Column(
          children: <Widget>[
            Expanded(child: Container()),
            Container(
              padding: EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10))
              ),
              child: Column(
                children: <Widget>[
                  TextField(
                    style: TextStyle(color: StaticVar.blueColor,),
                    cursorColor: StaticVar.blueColor,
                    cursorRadius: Radius.circular(0),
                    maxLength: 12,
                    decoration: InputDecoration(
                        labelText: "昵称",
                        hintText: "1~12位字符",
                        prefixIcon: Icon(Icons.face)
                    ),
                    onChanged: (val){
                      userName = val;
                    },
                  ),
                  Container(height: 5,),
                  RadioGroup(data: {'1':'男','0':'女',}, defaulVlue: sexValue, onChange: (val){sexValue = val;}),
                  TextField(
                    style: TextStyle(color: StaticVar.blueColor,),
                    maxLength: 16,
                    decoration: InputDecoration(
                        labelText: "密码",
                        hintText: "1~16位字符",
                        prefixIcon: Icon(Icons.lock),
                    ),
                    obscureText: true,
                    cursorColor: StaticVar.blueColor,
                    cursorRadius: Radius.circular(0),
                    onChanged: (val){
                      pwd = val;
                    },
                    onSubmitted: (str){
                      regist(context);
                    }
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 15),
                    child: LoadingGradientBtn(
                      exStream: this.btnLoadingStream,
                      color: StaticVar.blueColor,
                      width: 0,
                      iconColor: Colors.white,
                      child: Text('注册', style: TextStyle(color: Colors.white)),
                      pressed: (){
                        regist(context);
                      },
                    )
                  ),
                  Container(
                    padding: EdgeInsets.only(top: 10),
                    alignment: Alignment.centerLeft,
                    child: GestureDetector(
                      child: Text('<< 直接登录', style: TextStyle(fontSize: 13.5, color: StaticVar.yellowColor),),
                      onTap: (){
                        Navigator.of(context).pushReplacementNamed('/login');
                      },
                    )
                  )
                ],
              )
            ),
            Expanded(child: Container())
          ],
        )
      ),
    );
  }
}